Completed
Push — master ( 82a867...542b36 )
by greg
02:02
created

create.js ➔ ... ➔ ???   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 4
dl 0
loc 38
rs 8.439
c 0
b 0
f 0
nop 2
1
import path from 'path'
2
import {
3
  coreUtils,
4
  cmsTemplates,
5
  cmsOperations,
6
  config,
7
  abeExtend,
8
  cmsData,
9
  Manager
10
} from '../../'
11
12
const create = function(templateId, pathCreate, name, req, forceJson = {}, duplicate = false) {
13
  const p = new Promise((resolve, reject) => {
14
    abeExtend.hooks.instance.trigger('beforeCreate', templateId, pathCreate, name, req, forceJson)
15
16
    const templatePath = path.join(config.root, config.templates.url, `${templateId}.${config.files.templates.extension}`)
17
    let postDataPath = path.join(pathCreate, name)
18
    postDataPath = coreUtils.slug.clean(postDataPath)
19
    postDataPath = path.join(config.root, config.data.url, postDataPath)
20
21
    if(templatePath !== null && postDataPath !== null) {
22
      if(!coreUtils.file.exist(postDataPath)) {
23
        let postData = (forceJson) ? forceJson : {}
24
        let template = cmsTemplates.template.getTemplate(templatePath)
25
        if (duplicate) {
26
          postData = cmsData.values.removeDuplicate(template, postData)
27
        }
28
        template = cmsData.source.removeDataList(template)
29
        var resHook = abeExtend.hooks.instance.trigger('beforeFirstSave', postDataPath, req.query, postData, template)
30
        postDataPath = resHook.filePath
31
        postData = resHook.json
32
        template = resHook.text
33
34
        abeExtend.hooks.instance.trigger('afterCreate', postData, template, pathCreate, name, req, forceJson)
35
        cmsOperations.save.save(postDataPath, templateId, postData, template, 'draft', null, 'draft')
36
          .then((resSave) => {
37
            Manager.instance.updatePostInList(resSave.jsonPath)
38
            resolve(resSave.json)
39
          }).catch(function(e) {
40
            reject()
41
            console.error(e)
42
          })
43
      }else {
44
        postData = cmsData.file.get(postDataPath)
0 ignored issues
show
Bug introduced by
The variable postData seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.postData.
Loading history...
45
        resolve(postData, postDataPath)
46
      }
47
    }else {
48
      reject()
49
    }
50
  }).catch(function(e) {
51
    console.error(e)
52
  })
53
54
  return p
55
}
56
57
export default create